package com.demo.common.support.builder;

import com.demo.common.support.model.*;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * @author molong
 * @since 2024/5/24
 */
public class BaseResponseBuilder<T> {
    protected String message = "success";

    protected T data;

    protected Collection<T> datas;

    protected int code = 0;

    protected Map<String, Object> extend;

    /**
     * 实例化一个构建器
     *
     * @param <T> 数据类型
     * @return 构建器
     */
    public static <T> BaseResponseBuilder<T> start() {
        return new BaseResponseBuilder<>();
    }

    /**
     * 返回生成的实体
     *
     * @return 返回结果实体
     */
    public DataResponse<T> build() {
        BaseResponse<T> baseResponse;
        if (Objects.nonNull(this.datas)) {
            MultipleDataResponse<T> multipleDataResponse = new MultipleDataResponse<>();
            multipleDataResponse.setData(this.datas);
            baseResponse = multipleDataResponse;
        } else if (Objects.nonNull(this.data)) {
            SimpleDataResponse<T> dataResponse = new SimpleDataResponse<>();
            dataResponse.setData(this.data);
            baseResponse = dataResponse;
        } else {
            baseResponse = new EmptyDataResponse<>();
        }
        if (!ObjectUtils.isEmpty(extend)) {
            baseResponse.setExtend(extend);
        }
        baseResponse.setCode(this.code);
        baseResponse.setMessage(this.message);
        return baseResponse;
    }

    /**
     * 添加额外扩展数据
     *
     * @param extend 扩展数据
     * @return 构造器
     */
    public BaseResponseBuilder<T> extend(Map<String, Object> extend) {
        this.extend = extend;
        return this;
    }

    public BaseResponseBuilder<T> data(Collection<T> datas) {
        this.datas = datas;
        return this;
    }

    public BaseResponseBuilder<T> data(T data) {
        this.data = data;
        return this;
    }

    /**
     * 执行成功无任何消息提示并使用默认提示码{@code 0},{@link org.springframework.http.HttpStatus}特征{@code 200}
     *
     * @return 创建实体构建器
     */
    public BaseResponseBuilder<T> succeed() {
        return this;
    }

    /**
     * 执行成功带一个消息提示并使用默认提示码{@code 0},{@link org.springframework.http.HttpStatus}特征{@code 200}
     *
     * @param successMessage 消息提示语
     * @return 创建实体构建器
     */
    public BaseResponseBuilder<T> succeed(String successMessage) {
        this.message = successMessage;
        return this;
    }

    /**
     * 执行成功带一个消息提示和代码,{@link org.springframework.http.HttpStatus}特征{@code 200}
     *
     * @param successMessage 消息提示语
     * @param code           执行成功代码
     * @return 创建实体构建器
     */
    public BaseResponseBuilder<T> succeed(String successMessage, int code) {
        this.message = successMessage;
        this.code = code;
        return this;
    }

    /**
     * 执行失败带消息提示和错误代码,{@link org.springframework.http.HttpStatus}特征{@code 500}
     *
     * @param failureMessage 消息提示语
     * @param code           错误代码
     * @return 创建实体构建器
     */
    public BaseResponseBuilder<T> fail(String failureMessage, int code) {
        this.message = failureMessage;
        this.code = code;
        return this;
    }

    /**
     * 执行失败带消息提示并使用默认返回代码{@code -1},{@link org.springframework.http.HttpStatus}特征{@code 500}
     *
     * @param failureMessage 消息提示语
     * @return 创建实体构建器
     */
    public BaseResponseBuilder<T> fail(String failureMessage) {
        this.code = -1;
        this.message = failureMessage;
        return this;
    }

    /**
     * 执行失败不带消息提示并使用默认返回代码{@code -1},{@link org.springframework.http.HttpStatus}特征{@code 500}
     *
     * @return 创建实体构建器
     */
    public BaseResponseBuilder<T> fail() {
        this.code = -1;
        return this;
    }
}

